package org.codeart.map.remote;

import com.alibaba.fastjson.JSONObject;
import org.codeart.common.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class MapServiceClient {

    @Value("${system.map.key}")
    private String key;
    
    @Value("${system.map.track-service-add-api}")
    private String trackApi;

    @Autowired
    private RestTemplate restTemplate;
    
    public Integer addTrackService(String name) {
        Map<String, Object> conditions = new HashMap<String, Object>() {{
            put("key", key);
            put("name", name);
        }};
        String url = UrlUtil.createQueryParameters(trackApi, conditions);
        ResponseEntity<String> response = restTemplate.postForEntity(url, null, String.class);
        String body = response.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        return jsonObject.getJSONObject("data").getInteger("sid");
    }

}
